package com.wythe.mall.view.dialog;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.donkingliang.labels.LabelsView;
import com.wythe.mall.R;
import com.wythe.mall.model.SizeBean;

import java.util.ArrayList;
import java.util.List;

public class GoodSizePopupwindow extends PopupWindow {

    private final LabelsView labelsView;
    private final View minusView;
    private final View addView;
    private ImageView mIvRule;
    private TextView mPrice, mSku, mCount,mAddCar,mBuyShop,mShopAdd;
    private LinearLayout mLLBottom;
    private Context mContext;
    private View view;
    private List<SizeBean> mSizeList;
    private OnSelectTextListener mListener;


    public GoodSizePopupwindow(final Context mContext, List<SizeBean> list, View.OnClickListener itemsOnClick,OnSelectTextListener listener,int flag) {
        this.view = LayoutInflater.from(mContext).inflate(R.layout.popupwindow_goods_rule, null);
        mListener = listener;
        minusView = view.findViewById(R.id.goodsRule_minusRelative);
        addView = view.findViewById(R.id.goodsRule_addRelative);
        mShopAdd = view.findViewById(R.id.btn_confirm);
        mIvRule = view.findViewById(R.id.iv_rule);
        mPrice = view.findViewById(R.id.tv_price);
        mCount = view.findViewById(R.id.tv_count);
        mLLBottom = view.findViewById(R.id.ll_bottom);
        mSku = view.findViewById(R.id.tv_sku);
        mBuyShop = view.findViewById(R.id.tv_buy);
        mAddCar = view.findViewById(R.id.tv_add_car);
        labelsView = (LabelsView) view.findViewById(R.id.labels);
        mSizeList = list;
        mPrice.setText("￥ " + mSizeList.get(0).getPrice());
        mCount.setText("库存：" + mSizeList.get(0).getStockCount());
        mSku.setText(mSizeList.get(0).getTitle());
        Glide.with(mContext).load(mSizeList.get(0).getImg()).into(mIvRule);
        // 设置按钮监听
        minusView.setOnClickListener(itemsOnClick);
        addView.setOnClickListener(itemsOnClick);
        mBuyShop.setOnClickListener(itemsOnClick);
        mAddCar.setOnClickListener(itemsOnClick);
        mShopAdd.setOnClickListener(itemsOnClick);

        if (flag == 1 || flag == 2) {
            mShopAdd.setVisibility(View.VISIBLE);
            mLLBottom.setVisibility(View.GONE);
        } else {
            mShopAdd.setVisibility(View.GONE);
            mLLBottom.setVisibility(View.VISIBLE);
        }

        ArrayList<String> label = new ArrayList<>();
        for (int i = 0; i < mSizeList.size(); i++) {
            label.add(mSizeList.get(i).getTitle());
        }
        labelsView.setLabels(label); //直接设置一个字符串数组就可以了。
        labelsView.setSelects(0);
        labelsView.setOnLabelSelectChangeListener(new LabelsView.OnLabelSelectChangeListener() {
            @Override
            public void onLabelSelectChange(View label, String labelText, boolean isSelect, int position) {
                mPrice.setText("￥ " + mSizeList.get(position).getPrice());
                mCount.setText("库存：" + mSizeList.get(position).getStockCount());
                mSku.setText(mSizeList.get(position).getTitle());
                Glide.with(mContext).load(mSizeList.get(position).getImg()).into(mIvRule);
                mListener.onSelectTextStatus(position,labelText);
            }
        });
//        ======================================================
        // 设置外部可点击
        this.setOutsideTouchable(true);
        // mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        this.view.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                int height = view.findViewById(R.id.pop_layout).getTop();

                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });


        /* 设置弹出窗口特征 */
        // 设置视图
        this.setContentView(this.view);
        // 设置弹出窗体的宽和高
        this.setHeight(RelativeLayout.LayoutParams.WRAP_CONTENT);
        this.setWidth(RelativeLayout.LayoutParams.MATCH_PARENT);

        // 设置弹出窗体可点击
        this.setFocusable(true);
        this.setBackgroundDrawable(new BitmapDrawable());

        // 设置弹出窗体显示时的动画，从底部向上弹出
        this.setAnimationStyle(R.style.take_photo_anim);

    }

    public interface OnSelectTextListener {
        void onSelectTextStatus(int position,String title);
    }

}

